//
//  CLLocation+FK.m
//  智慧水利平台
//
//  Created by Macbook Pro on 2022/4/6.
//  Copyright © 2022 jxwdyf. All rights reserved.
//

#import "CLLocation+FK.h"


const double a = 6378245.0;
const double ee = 0.00669342162296594323;
#define xPi (pi * 3000.0 / 180.0)

@implementation CLLocation (FK)


+ (CLLocationCoordinate2D)wgs84_To_gcj02:(double)lng lat:(double)lat
{
    double dLat = [self transformLat:lng - 105.0 y:lat - 35.0];
    double dLng = [self transformLng:lng - 105.0 y:lat - 35.0];
    double radLat = lat / 180.0 * M_PI;
    double magic = sin(radLat);
    magic = 1 - ee * magic * magic;
    double sqrtMagic = sqrt(magic);
    dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * M_PI);
    dLng = (dLng * 180.0) / (a / sqrtMagic * cos(radLat) * M_PI);
    double mgLat = lat + dLat;
    double mgLng = lng + dLng;
    return CLLocationCoordinate2DMake(mgLat, mgLng);
}

+ (CLLocationCoordinate2D)gcj02_To_wgs84:(double)lng lat:(double)lat
{
    CLLocationCoordinate2D coord = [self wgs84_To_gcj02:lng lat:lat];
    double lontitude = lng * 2 - coord.longitude;
    double latitude = lat * 2 - coord.latitude;
    return CLLocationCoordinate2DMake(latitude, lontitude);
}

+ (double)transformLat:(double)x y:(double)y
{
    double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * sqrt(ABS(x));
    ret += (20.0 * sin(6.0 * x * M_PI) + 20.0 * sin(2.0 * x * M_PI)) * 2.0 / 3.0;
    ret += (20.0 * sin(y * M_PI) + 40.0 * sin(y / 3.0 * M_PI)) * 2.0 / 3.0;
    ret += (160.0 * sin(y / 12.0 * M_PI) + 320 * sin(y * M_PI / 30.0)) * 2.0 / 3.0;
    return ret;
}

+ (double)transformLng:(double)x y:(double)y
{
    double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * sqrt(ABS(x));
    ret = ret + (20.0 * sin(6.0 * x * M_PI) + 20.0 * sin(2.0 * x * M_PI)) * 2.0 / 3.0;
    ret += (20.0 * sin(x * M_PI) + 40.0 * sin(x / 3.0 * M_PI)) * 2.0 / 3.0;
    ret += (150.0 * sin(x / 12.0 * M_PI) + 300.0 * sin(x / 30.0 * M_PI)) * 2.0 / 3.0;
    return ret;
}

//this.gps84_To_gcj02 = function (lng, lat) {
//  let dLat = transformLat(lng - 105.0, lat - 35.0)
//  let dLng = transformLng(lng - 105.0, lat - 35.0)
//  const radLat = lat / 180.0 * pi
//  let magic = Math.sin(radLat)
//  magic = 1 - ee * magic * magic
//  const sqrtMagic = Math.sqrt(magic)
//  dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi)
//  dLng = (dLng * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi)
//  const mgLat = lat + dLat
//  const mgLng = lng + dLng
//  return {
//    lng: mgLng,
//    lat: mgLat
//  }
//}
/** 火星转84 */
//this.gcj02_To_gps84 = function (lng, lat) {
//  const coord = this.gps84_To_gcj02(lng, lat)
//  const lontitude = lng * 2 - coord.lng
//  const latitude = lat * 2 - coord.lat
//  return {
//    lng: lontitude,
//    lat: latitude
//  }
//}
//function transformLat (x, y) {
//  let ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x))
//  ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0
//  ret += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0
//  ret += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0
//  return ret
//}
//function transformLng (x, y) {
//  let ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x))
//  ret = ret + (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0
//  ret += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0
//  ret += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0 * pi)) * 2.0 / 3.0
//  return ret
//}

@end
